package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.dk.config.AlipayConfig;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.po.AlipayBaseResult;
import com.dk.po.AlipayRefundBaseResult;
import com.dk.po.AlipayRefundResultPO;
import com.dk.po.AlipayResultPO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("tradeController")
public class TradeController {

    //支付
    public String toAliPay(String WIDout_trade_no,String WIDtotal_amount,String WIDsubject,String WIDbody){
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //同步回调路径
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        //异步回调路径
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);


        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDout_trade_no +"\","
                + "\"total_amount\":\""+ WIDtotal_amount +"\","
                + "\"subject\":\""+ WIDsubject +"\","
                + "\"body\":\""+ WIDbody +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        //输出
//        out.println(result);
        return result;
    }

    //交易查询
    @ApiOperation(value = "交易查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "WIDTQout_trade_no",value = "交易流水号",required = true),
            @ApiImplicitParam(name = "WIDout_trade_no",value = "秒杀订单号",required = true)
    })
    @GetMapping("alipayTradeQuery")
    public Result alipayTradeQuery(String WIDTQout_trade_no, String WIDTQtrade_no){
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTQout_trade_no +"\","+"\"trade_no\":\""+ WIDTQtrade_no +"\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
            AlipayBaseResult alipayBaseResult = JSONObject.parseObject(result,AlipayBaseResult.class);
            AlipayResultPO response = alipayBaseResult.getAlipay_trade_query_response();
            if (response == null){
                return Result.bulid(ConstantEnum.ERROR).setResult("支付宝查询失败，没有数据");
            }
            if (response != null && response.getCode().intValue() == 10000){
                return Result.bulid(ConstantEnum.SUCCESS).setResult(response);
            }
            return Result.bulid(ConstantEnum.ERROR);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return Result.bulid(ConstantEnum.ERROR);
        }
    }

    /*
    *WIDTRout_trade_no   订单号
    *WIDTRtrade_no       流水号
    * WIDTRrefund_amount    退款金额
    * WIDTRrefund_reason     退款原因
    * WIDTRout_request_no     退款请求号
    * */
    @ApiOperation(value = "退款接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "WIDout_trade_no",value = "订单号",required = true),
            @ApiImplicitParam(name = "WIDTRtrade_no",value = "流水号",required = true),
            @ApiImplicitParam(name = "WIDTRrefund_amount",value = "退款金额",required = true),
            @ApiImplicitParam(name = "WIDTRrefund_reason",value = "退款原因",required = true),
            @ApiImplicitParam(name = "WIDTRout_request_no",value = "退款请求号",required = true)
    })
    @GetMapping(value = "alipayTradeRefund")
    public Result alipayTradeRefund(String WIDTRout_trade_no,String WIDTRtrade_no,String WIDTRrefund_amount,
                                    String WIDTRrefund_reason,String WIDTRout_request_no){
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTRout_trade_no +"\","
                + "\"trade_no\":\""+ WIDTRtrade_no +"\","
                + "\"refund_amount\":\""+ WIDTRrefund_amount +"\","
                + "\"refund_reason\":\""+ WIDTRrefund_reason +"\","
                + "\"out_request_no\":\""+ WIDTRout_request_no +"\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
            return Result.bulid(ConstantEnum.SUCCESS).setResult(result);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return Result.bulid(ConstantEnum.ERROR);
        }
    }

    @ApiOperation(value = "退款查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "WIDRQout_trade_no",value = "订单号",required = true),
            @ApiImplicitParam(name = "WIDRQtrade_no",value = "流水号",required = true),
            @ApiImplicitParam(name = "WIDRQout_request_no",value = "退款请求号",required = true)
    })
    @GetMapping(value = "alipayTradeFastpayRefundQuery")
    public Result alipayTradeFastpayRefundQuery(String WIDRQout_trade_no,String WIDRQtrade_no,String WIDRQout_request_no){
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();


        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDRQout_trade_no +"\","
                +"\"trade_no\":\""+ WIDRQtrade_no +"\","
                +"\"out_request_no\":\""+ WIDRQout_request_no +"\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
            AlipayRefundBaseResult alipayRefundBaseResult = JSONObject.parseObject(result, AlipayRefundBaseResult.class);
            AlipayRefundResultPO response = alipayRefundBaseResult.getAlipay_trade_fastpay_refund_query_response();
            if (response == null){
                return Result.bulid(ConstantEnum.ERROR).setResult("退款失败，未找到数据");
            }
            if (response != null && response.getCode().intValue() == 10000){
                return Result.bulid(ConstantEnum.SUCCESS).setResult(response);
            }
            return Result.bulid(ConstantEnum.ERROR);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return Result.bulid(ConstantEnum.ERROR);
        }
    }
}
